<template>
  <VPage edit class="edit">
    <!-- 备货规则设置 -->
    <RuleConfig ref="RuleConfigRef" :info="formData" />
    <!-- 预估销量设置 -->
    <el-card class="box-card inline-form">
      <FormTitle title="预估销量设置" />
      <div class="flex items-center flex-wrap mb-[8px] gap-y-2">
        加权日平均销量=
        <span v-for="(item, i) in formData.predict_sales.average_sales" :key="i">
          <el-input v-model="item.day_start" placeholder="天" clearable /> - <el-input v-model="item.day_end" placeholder="天" clearable />天销量 /
          <el-input v-model="item.day_num" placeholder="天" clearable />* <el-input v-model="item.percent" placeholder="比例" clearable />%
          {{ i + 1 >= formData.predict_sales.average_sales.length ? "" : "+" }}
        </span>
      </div>
      <p>说明：如1-30天销量/30*50%+31-60天销量/30*30%+61-90天销量/30*20%，比例参数根据实际情况填写。</p>
    </el-card>
    <!-- 销量系数设置 -->
    <SalesCoefficient ref="SalesCoefficientRef" :info="formData" />
    <!-- 智能补货配置 -->
    <Replenishment ref="ReplenishmentRef" :info="formData" />
    <!-- 备货计划明细自动审核设置 -->
    <AutoAudit ref="AutoAuditRef" :info="formData" />
    <template #footer>
      <el-button @click="queryForm"> 取消 </el-button>
      <el-button type="primary" @click="handleSave"> 保存 </el-button>
    </template>
  </VPage>
</template>

<script setup>
import { predictSalesSave, predictSalesInfo } from "@/api/base"

import { ElMessage, ElMessageBox } from "element-plus"
import { onMounted, ref, toRefs } from "vue"
import XForm from "@/components/XForm/index.vue"
import FormTitle from "@/components/XForm/FormTitle.vue"

import RuleConfig from "./components/ruleConfig.vue"
const RuleConfigRef = ref()
import SalesCoefficient from "./components/salesCoefficient.vue"
const SalesCoefficientRef = ref()
import Replenishment from "./components/replenishment.vue"
const ReplenishmentRef = ref()
import AutoAudit from "./components/autoAudit.vue"
const AutoAuditRef = ref()

const explain = [
  "备货天数：系统检测到需要补货时，系统计算默认需求数量；",
  "基础LT天数 = 采购处理天数 + 采购天数 + 本地仓处理天数 + 运输天数 + 海外中转仓处理天数 + 平台上架天数 + 安全天数；",
  "采购处理天数：采购下单与公司内部审核流程的时间；",
  "采购天数：包含采购下单，供应商生产，供应商运输天数；",
  "本地仓处理天数：国内本地仓入库、贴标、打包出库的天数；",
  "运输天数：头程运输到平台仓库签收的天数；",
  "海外中转仓处理天数 : 海外仓中转仓入库、贴标、打包出库的天数；",
  "平台上架天数 ：平台仓库入库上架的天数；",
  "安全天数：为防止断货，增加额外备货库存；",
]

const data = reactive({
  formData: {
    // 备货规则设置
    stocking_rules: {
      transport_config: [], // 运输天数配置信息
    },

    predict_sales: {
      //预估销量设置
      average_sales: [{}, {}, {}],
    },
    sales_coefficient: {}, //销量系数设置
    intelligent_stocking: {}, //智能补货配置
    auto_verify: {}, //备货计划明细自动审核设置
  },
})
const { formData } = toRefs(data)

//获取下拉数据
onMounted(() => {
  queryForm()
})
const queryForm = () => {
  predictSalesInfo().then((res) => {
    if (!res.data.stocking_rules?.transport_config?.length) {
      res.data.stocking_rules.transport_config = [{}]
    }
    if (!res.data.intelligent_stocking?.stocking_days_config?.length) {
      res.data.intelligent_stocking.stocking_days_config = [{}]
    }

    formData.value = res.data
  })
}
const hasDuplicates = (array) => {
  return new Set(array).size !== array.length
}
const handleSave = async () => {
  try {
    await RuleConfigRef.value.form.submit()
    await SalesCoefficientRef.value.form.submit()
    await ReplenishmentRef.value.form.submit()
    await AutoAuditRef.value.form.submit()
    if (hasDuplicates(formData.value.stocking_rules?.platform_issued_days.map((v) => v.site_code))) {
      return ElMessage.warning("国家不能重复")
    }
    if (eval(formData.value?.predict_sales?.average_sales?.map((v) => v?.percent || 0)?.join("+")) != 100) {
      return ElMessage.warning("百分数的和必须等于1")
    }
    predictSalesSave(formData.value).then((res) => {
      ElMessage.success("保存成功")
    })
  } catch (e) {
    console.warn(e)
  }
}
</script>
<style lang="scss" scoped>
.box-card {
  margin-bottom: 8px;
}
.inline-form {
  .el-input {
    width: 60px;
  }
}
</style>
